package com.ysj.demo.accompanynetwork.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.ysj.demo.accompanynetwork.ApiServiceUntil;
import com.ysj.demo.accompanynetwork.R;
import com.ysj.demo.accompanynetwork.util.SpUtil;
import com.ysj.demo.accompanynetwork.util.StatusBarUtil;
import com.ysj.demo.accompanynetwork.adapter.ItemIntegralAdapter;
import com.ysj.demo.accompanynetwork.bean.IntegralRecordResult;
import com.ysj.demo.accompanynetwork.bean.IntegralRecordBean;
import com.ysj.demo.accompanynetwork.bean.IntegralRecordRequest;
import com.ysj.demo.accompanynetwork.util.ToolUtil;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 80530 on 2018/8/8.
 */

public class AllPublishFragment extends Fragment implements View.OnClickListener {

    private View view;
    private PullToRefreshLayout pullToRefreshLayout;
    private RecyclerView recyclerView;
    private ItemIntegralAdapter adapter;
    private List<IntegralRecordBean> list;

    private String userid;
    private int currentPage = 1;

    public AllPublishFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_list_view_layout, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public void onDetach() {
        super.onDetach();

    }

    private void initView() {
        list = new ArrayList<>();
        recyclerView = view.findViewById(R.id.rec_lv);
        pullToRefreshLayout = view.findViewById(R.id.activity_main);
        final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());

        adapter = new ItemIntegralAdapter(getContext(), list);

        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        //设置Adapter
        recyclerView.setAdapter(adapter);
//        //设置分割线
//        //添加自定义分割线
//        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
//        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.custom_divider));
//        recyclerView.addItemDecoration(divider);
        //设置增加或删除条目的动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());

//        initData();
        pullToRefreshLayout.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                loadTask(1);
                pullToRefreshLayout.finishRefresh();
            }

            @Override
            public void loadMore() {
                loadTask(currentPage);
                // 结束加载更多
                pullToRefreshLayout.finishLoadMore();
            }
        });
    }

    private void initData() {
        userid = (String) SpUtil.getParam(getContext().getApplicationContext(), SpUtil.USER_ID, "");
        loadTask(1);
    }

    public void loadTask(final int mCurrentPage) {
        final IntegralRecordRequest bean = new IntegralRecordRequest();
        bean.setUserid(userid);
        bean.setPagesize(5);
        bean.setPagenum(mCurrentPage);
        bean.setType(0);
        Call<IntegralRecordResult> validCall =ApiServiceUntil.getInstance().getmApi().getIntegralRecord(bean);
        validCall.enqueue(new Callback<IntegralRecordResult>() {
            @Override
            public void onResponse(Call<IntegralRecordResult> call, Response<IntegralRecordResult> response) {
                IntegralRecordResult body = response.body();
                if (body.getCode() == 0) {
                    if (currentPage == 1) {
                        adapter.clearList();
                        return;
                    }
                    currentPage = +1;
                    ArrayList<IntegralRecordBean> list = body.getData().getRecords();
                    adapter.addList(list);
                    ToolUtil.ToastShow(getActivity().getApplication(), "成功");

                } else {
                    ToolUtil.ToastShow(getActivity().getApplication(), "失败");

                }
                Log.i("PersonalCenterActivity", new Gson().toJson(body));
            }

            @Override
            public void onFailure(Call<IntegralRecordResult> call, Throwable t) {
                Log.i("PersonalCenterActivity", t.toString());
            }
        });
    }
}
